<template>
  <div class="app-container">
    <div class="custom_card_0">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="推送标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入推送标题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="模板名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入模板名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in dict.type.sys_available"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['message:mmstemplate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['message:mmstemplate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    </div>
    <div class="custom_card">
      <el-table
        v-loading="loading"
        :data="mmstemplateList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          label="模板名称"
          align="center"
          prop="name"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="推送标题"
          align="center"
          prop="title"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="推送节点"
          align="center"
          prop="pushNode"
          min-width="160"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.push_node"
              :value="scope.row.pushNode"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="模板内容"
          align="center"
          prop="content"
          :formatter="formatter"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="状态"
          align="center"
          prop="status"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_available"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="模板参数"
          align="center"
          prop="param"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="160"
          fixed="left"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.status == 0"
              v-hasPermi="['message:mmstemplate:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleEnable(scope.row)"
              v-if="scope.row.status == 0"
              v-hasPermi="['message:mmstemplate:edit']"
              >启用</el-button
            >

            <el-button
              size="mini"
              type="text"
              @click="handleDeactivate(scope.row)"
              v-if="scope.row.status == 1"
              v-hasPermi="['message:mmstemplate:edit']"
              >停用</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改消息模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-row :gutter="15">
        <el-form
          ref="form"
          :model="formData"
          :rules="rules"
          size="medium"
          label-width="100px"
          label-position="top"
        >
          <el-col :span="12">
            <el-form-item label="模板名称" prop="name">
              <el-input
                v-model="formData.name"
                placeholder="请输入模板名称"
                :maxlength="30"
                show-word-limit
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送标题" prop="title">
              <el-input
                v-model="formData.title"
                placeholder="请输入推送标题"
                :maxlength="30"
                show-word-limit
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div style="width: 100%; height: 385px; border: 1px #dcdfe6 solid">
              <el-row :gutter="15">
                <el-col :span="7" style="height: 385px">
                  <el-form-item label="模板参数" prop="param">
                    <div
                      v-for="dict in dict.type.sms_template"
                      :key="dict.value"
                      style="display: inline-block"
                    >
                      <div
                        v-for="item in optionaParams"
                        :key="item"
                        style="display: inline-block"
                      >
                        <el-button
                          @click="insertParam(dict.label, dict.value)"
                          v-if="dict.value == item"
                          class="param_btn"
                          >{{ dict.label }}</el-button
                        >
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col
                  :span="17"
                  style="height: 385px; border-left: 1px #dcdfe6 solid"
                >
                  <el-form-item label="模板内容" prop="content">
                    <el-input
                      v-model="formData.content"
                      type="textarea"
                      placeholder="请输入模板内容"
                      :maxlength="500"
                      show-word-limit
                      :autosize="{ minRows: 15, maxRows: 15 }"
                      :style="{ width: '100%' }"
                      class="this_textarea"
                    ></el-input>
                    <!-- <editor v-model="formData.content" :height="304"  :showToolbar="true"/> -->
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMmstemplate,
  getMmstemplate,
  editStatus,
  addMmstemplate,
  updateMmstemplate,
  exportMmstemplate,
} from '@/api/message/mmstemplate'

export default {
  name: 'Mmstemplate',
  dicts: ['sys_available', 'sms_template', 'push_node'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      optionaParams: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息模板表格数据
      mmstemplateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        name: null,
        status: null,
      },
      formData: {
        name: '',
        title: '',
        param: [],
        content: '',
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入模板名称',
            trigger: 'blur',
          },
        ],
        title: [
          {
            required: true,
            message: '请输入推送标题',
            trigger: 'blur',
          },
        ],
        content: [
          {
            required: true,
            message: '请输入模板内容',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatter(row, column, cellValue, index) {
      var paramArr = row.param?.split(',')
      for (let param of paramArr) {
        cellValue = cellValue.replace('{}', '[' + param + ']')
      }
      return cellValue
    },
    insertParam(label, value) {
      this.formData.content += '{' + value + '}'
      this.formData.param.push(value)
    },
    /** 查询消息模板列表 */
    getList() {
      this.loading = true
      listMmstemplate(this.queryParams).then((response) => {
        this.mmstemplateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formData = {
        id: null,
        name: '',
        title: '',
        param: [],
        content: '',
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加消息模板'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMmstemplate(id).then((response) => {
        this.formData = response.data
        this.formData.param = response.data.param.split(',')
        for (let param of this.formData.param) {
          this.formData.content = this.formData.content.replace(
            '{}',
            '{' + param + '}'
          )
        }
        this.optionaParams = response.data.optionaParam.split(',')
        this.open = true
        this.title = '修改消息模板'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let data = {
            ...this.formData,
            param: this.formData.param.join(','),
            optionaParam: this.optionaParams.join(','),
          }
          if (this.formData.id != null) {
            updateMmstemplate(data).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMmstemplate(data).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 启用按钮操作 */
    handleEnable(row) {
      this.$modal
        .confirm('是否确认启用消息模板数据项？')
        .then(function () {
          return editStatus(row.id, 1)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('启用成功')
        })
        .catch(() => {})
    },
    /** 停用按钮操作 */
    handleDeactivate(row) {
      this.$modal
        .confirm('是否确认停用消息模板数据项？')
        .then(function () {
          return editStatus(row.id, 0)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('停用成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有消息模板数据项？')
        .then(() => {
          this.exportLoading = true
          return exportMmstemplate(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
<style scoped="scoped">
/deep/ .this_textarea textarea {
  resize: none;
}

.param_btn {
  width: 100px;
  margin: 5px;
}
</style>
